public class BinarySearchTree {

    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }

        @Override
        public String toString() {
            return "TreeNode{" +
                    "key=" + key +
                    '}';
        }
    }

    public TreeNode root;
    /**
     * 插入一个元素
     *
     * @param key
     */
    public boolean insert(int key) {
        TreeNode node = new TreeNode(key);
        if (root == null) {
            root = node;
            return true;
        }
        if (search(key) == null) {
            TreeNode parent = null;
            TreeNode cur = root;
            while (cur != null) {
                if (cur.key < key) {
                    parent = cur;
                    cur = cur.right;
                } else {
                    parent = cur;
                    cur = cur.left;
                }
            }
            if (parent.left == cur) {
                parent.left = node;
            } else {
                parent.right = node;
            }
            return true;
        }
        return false;
    }

    //查找key是否存在
    public TreeNode search(int key) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.key < key) {
                cur = cur.right;
            } else if (cur.key > key) {
                cur = cur.left;
            } else {
                return cur;
            }
        }
        return null;
    }

    //删除key的值
    public boolean remove(int key) {
        if (search(key) == null) {
            return false;
        }
        TreeNode parent = null;
        TreeNode cur = root;
        while (cur.key != key) {
            if (cur.key < key) {
                parent = cur;
                cur = cur.right;
            } else {
                parent = cur;
                cur = cur.left;
            }
        }
        removeNode(parent,cur);
        return true;
    }

    private void removeNode(TreeNode parent, TreeNode cur) {
        if(cur.left == null){
            if(cur == root){
                root = cur.right;
            }else if(parent.left == cur){
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if(cur.right == null){
            if(cur == root){
                root = cur.left;
            }else if(parent.left == cur){
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            TreeNode target = cur.right;
            TreeNode targetP = cur;
            while(target.left != null){
                targetP = target;
                target = target.left;
            }
            cur.key = target.key;
            if(targetP.left == target){
                targetP.left = target.right;
            }else {
                targetP.right = target.right;
            }
        }
    }

}